﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Management;

namespace csexplorer
{
    public class CsExplorerManager
    {
        public List<DiskEntity> GetRoodFolderContent()
        {
            List<DiskEntity> fileFolders = new List<DiskEntity>();
            List<DriveInformation> drives = GetDrives();

            foreach (DriveInformation drv in drives)
            {
                fileFolders.Add(drv);
            }

            return fileFolders;
        }

        public List<DirectoryInformation> GetFolders(string folder)
        {
            List<DirectoryInformation> listFolders = new List<DirectoryInformation>();
            folder = (folder.EndsWith("\\"))? folder : folder + "\\";

            try
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(folder);
                DirectoryInfo[] folders = directoryInfo.GetDirectories();

                foreach (DirectoryInfo f in folders)
                {
                    string name = Path.GetFileName(f.Name);
                    DirectoryInformation di = new DirectoryInformation(name);
                    di.Path = f.FullName;
                    listFolders.Add(di);
                }
            }
            catch (UnauthorizedAccessException ex)
            {
            }
            catch (IOException ex)
            {
            }

            return listFolders;
        }

        public List<FileInformation> GetFiles(string folder)
        {
            List<FileInformation> listFiles = new List<FileInformation>();
            folder = (folder.EndsWith("\\")) ? folder : folder + "\\";

            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(folder);
                FileInfo[] files = dirInfo.GetFiles();

                foreach (FileInfo f in files)
                {
                    string name = f.Name;
                    FileInformation fi = new FileInformation(name);
                    fi.Path = folder;
                    listFiles.Add(fi);
                }
            }
            catch (UnauthorizedAccessException ex)
            {
            }
            catch (IOException ex)
            {
            }

            return listFiles;
        }

        private List<DriveInformation> GetDrives()
        {
            //get drive collection
            ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * From Win32_LogicalDisk ");
            ManagementObjectCollection queryCollection = query.Get();

            List<DriveInformation> drives = new List<DriveInformation>();

            foreach (ManagementObject mo in queryCollection)
            {
                string name = mo["Name"].ToString();
                DriveInformation drive = new DriveInformation(name);
                drive.Path = name;

                DriveType drvType;

                switch (int.Parse(mo["DriveType"].ToString()))
                {
                    case 2:			//removable drives
                        drvType = DriveType.Removable;
                        break;
                    case 3:			//Local drives
                        drvType = DriveType.LocalDisk;
                        break;
                    case 5:				//CD rom drives
                        drvType = DriveType.CD;
                        break;
                    case 4:			//Network drives
                        drvType = DriveType.Network;
                        break;
                    default:		//defalut to folder
                        drvType = DriveType.LocalDisk;
                        break;
                }

                drive.DriveType = drvType;

                drives.Add(drive);
            }

            return drives;
        }
    }
}
